package com.xpec.cipher.tals.record;

import com.xpec.cipher.tals.TalsConstance;

public class RSAPublicKeyRecord extends HandShakeRecord {
	private byte[] rsaKeyModulus = new byte[TalsConstance.RSA_MODULUS_SIZE];

	private byte[] rsaKeyExp = new byte[TalsConstance.RSA_EXP_SIZE];

	private byte cipherAlgo = TalsConstance.DATA_ENCRYPT_ALGORITHM_NULL;

	private byte hmacAlgo = TalsConstance.HMAC_ALGORITHM_NULL;

	public RSAPublicKeyRecord(byte[] rsaKeyModulus, byte[] rsaKeyExp,
			byte cipherAlgo, byte hmacAlgo) {
		this.rsaKeyModulus = rsaKeyModulus;
		this.rsaKeyExp = rsaKeyExp;
		this.cipherAlgo = cipherAlgo;
		this.hmacAlgo = hmacAlgo;
	}

	@Override
	protected int getContentLengthWithoutHandShakeTypeSize() {
		return TalsConstance.RSA_MODULUS_SIZE + TalsConstance.RSA_EXP_SIZE
				+ TalsConstance.BYTE_SIZE + TalsConstance.BYTE_SIZE
				+ TalsConstance.BYTE_SIZE;
	}

	@Override
	protected byte getHandShakeType() {
		return TalsConstance.HAND_SHAKE_TYPE_SERVER_RSA_PUBLIC_KEY;
	}

	@Override
	protected byte[] resolveSubStructWithoutHandShakeType() {
		
		byte[] buffer = new byte[TalsConstance.RSA_MODULUS_SIZE + TalsConstance.RSA_EXP_SIZE + 3]; 
		
		int offset = 0;
		for (int i = 0; i < TalsConstance.RSA_MODULUS_SIZE; i++) {
			buffer[offset++] = rsaKeyModulus[i];
		}
		for (int i = 0; i < TalsConstance.RSA_EXP_SIZE; i++) {
			buffer[offset++] = rsaKeyExp[i];
		}
		buffer[offset++] = hmacAlgo;
		buffer[offset++] = TalsConstance.HMAC_SIZE;
		buffer[offset++] = cipherAlgo;
		
		return buffer;
	}
}
